import json
import time
import pathway as pw

from application.cursor_model.file_cursor import FileCursorManager
from application.db.mongo_db.mongo_db_manager import MongoDBDataStream


class BaseMongoModel(pw.io.python.ConnectorSubject):
    collection = None
    topic = None
    batch_size = 1000
    sort_key = "_id"
    full_amount = False
    query = {}
    fields = {}
    max_limit = None

    def run(self):
        if not self.collection or not self.topic:
            raise Exception("collection or topic is required")
        cursor = FileCursorManager(
            collection=self.collection,
            topic=self.topic,
            full_amount=self.full_amount
        )

        mongodb_stream = MongoDBDataStream(
            collection=self.collection,
            batch_size=self.batch_size,
            sort_key=self.sort_key,
            historical_cursor_position=cursor.load()  # 加载历史游标位置
        )
        print("开始监听数据...")
        try:
            for doc in mongodb_stream.stream(query=self.query, fields=self.fields, max_limit=self.max_limit):
                # 更新游标位置
                mongodb_stream.cursor_filter = doc.get(self.sort_key)

                doc = self.transform(doc)
                self.next(**doc)
        except Exception as e:
            raise
        finally:
            cursor.save(mongodb_stream.cursor_filter)

    def transform(self, transform_data):
        return transform_data

    @property
    def _deletions_enabled(self) -> bool:
        return False
